//
//  DrawViewController.m
//  Leisure
//
//  Created by 大展 on 16/4/18.
//  Copyright © 2016年 张展. All rights reserved.
//

#import "DrawViewController.h"

@interface DrawViewController ()<LeftViewControllerDelegate>

@property (nonatomic, strong) LeftViewController *leftViewController;
@property (nonatomic, assign,getter=isShowing) BOOL show;

// 显示左视图
- (void)_showLeftViewController;

// 隐藏左视图
- (void)_hideLeftVieController;

// 添加视图控制器
- (void)_setupControllers;

// 添加视图
- (void)_setupViews;

// 添加平移手势
- (void)_addPanGesture;

@end

@implementation DrawViewController

#pragma mark -----LeftViewControllerDelegate协议-----

+ (DrawViewController *)sharedManager {

    static DrawViewController *handle = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{

        handle = [[DrawViewController alloc]init];
    });
    return handle;
}

- (void)leftViewController:(LeftViewController *)leftViewController clickAtIndexPath:(NSIndexPath *)indexpath {


    UINavigationController *navigation = self.mainViewController.navigationController;
    UIViewController *viewController = self.viewControllers[indexpath.row];

    [UIView animateWithDuration:.5 animations:^{

        CGRect frame = navigation.view.frame;
        frame.origin.x = self.view.frame.size.width;
        navigation.view.frame = frame;

    } completion:^(BOOL finished) {

        // 根据tag值来删除指定的视图(不包括navigation自带的view)
        for (int i = 0; i < self.mainViewController.view.subviews.count; i++) {

            if (self.mainViewController.view.subviews[i].tag  >= 100) {

                [self.mainViewController.view.subviews[i] removeFromSuperview];
            }
        }

        // 将指定的视图插入到navigationBar的下面
        [self.mainViewController.view addSubview:viewController.view];
//        [navigation.view insertSubview:viewController.view belowSubview:navigation.navigationBar];

        [self menuItemClickAction:nil];
    }];
}

- (NSMutableArray *)viewControllers {

    if (!_viewControllers) {

        _viewControllers = [NSMutableArray array];
    }
    return _viewControllers;
}

- (LeftViewController *)leftViewController {
    if (!_leftViewController) {

        _leftViewController = [[LeftViewController alloc]init];
        _leftViewController.dataSouce = @[@"阅读",@"电台",@"话题",@"良品"];
        _leftViewController.delegate = self;
    }
    return _leftViewController;
}

- (MainViewController *)mainViewController {

    if (!_mainViewController) {

        _mainViewController = [[MainViewController alloc]init];
        UIBarButtonItem *menuItem = [[UIBarButtonItem alloc]initWithTitle:@"菜单" style:(UIBarButtonItemStylePlain) target:self action:@selector(menuItemClickAction:)];

        _mainViewController.navigationItem.leftBarButtonItem = menuItem;
    }
    return _mainViewController;
}

// 添加视图控制器
- (void)_setupControllers {

    [self addChildViewController:self.leftViewController];
    UINavigationController *navigation = [[UINavigationController alloc]initWithRootViewController:self.mainViewController];
    [self addChildViewController:navigation];
}

// 添加视图
- (void)_setupViews {

    [self _setupControllers];
    [self.view addSubview:self.leftViewController.view];
    [self.view addSubview:self.mainViewController.navigationController.view];
    [self _addPanGesture];
}

// menuItem点击事件
- (void)menuItemClickAction:(UIBarButtonItem *)sender {

    self.show = !self.show;
    if (self.show) {

        [self _showLeftViewController];
    } else {

        [self _hideLeftVieController];
    }
}

// 显示左视图控制器
- (void)_showLeftViewController {

    UINavigationController *navigation = self.mainViewController.navigationController;
    [UIView animateWithDuration:.5 animations:^{

        CGRect frame = navigation.view.frame;
        frame.origin.x = self.view.frame.size.width *0.6;
        navigation.view.frame = frame;
    }];
}

// 添加平移手势
- (void)_addPanGesture {

    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGestureAction:)];
    [self.view addGestureRecognizer:panGesture];
}

// 平移手势回调方法
- (void)panGestureAction:(UIPanGestureRecognizer *)sender {

    CGPoint transition = [sender translationInView:self.view];
    UINavigationController *navigation = self.mainViewController.navigationController;
    CGPoint center = navigation.view.center;
    center.x +=  transition.x;
    if (navigation.view.frame.origin.x == 0 && transition.x < 0 ) {

        return;
    }

    if (sender.state == UIGestureRecognizerStateEnded) {


        if (navigation.view.frame.origin.x < self.view.frame.size.width*0.6) {
            self.show = NO;
            [self _hideLeftVieController];
        } else {

            self.show = YES;
            [self _showLeftViewController];
        }
        return;
    }
    navigation.view.center = center;
    [sender setTranslation:CGPointZero inView:self.view];

    if (navigation.view.center.x <= self.view.frame.size.width/2) {

        navigation.view.center = self.view.center;

    }
}
// 隐藏左视图控制器
- (void)_hideLeftVieController {
    UINavigationController *navigation = self.mainViewController.navigationController;
    [UIView animateWithDuration:.5 animations:^{

        CGRect frame = navigation.view.frame;
        frame.origin.x = 0;
        navigation.view.frame = frame;
    }];
}

- (void)loadView {

    [super loadView];
    [self _setupViews];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    // 给左视图控制上所有的控制器设置tag为了添加的视图时候方便根据tag值移除视图
    for (int i = 0; i < self.viewControllers.count; i++) {

        [[self.viewControllers[i] view] setTag:100+i];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
